/**
 * Copyright (C), 2020-2020, 软件公司
 * FileName: ProcessInstanceQuery.java
 * Author:   cakin
 * Date:     2020/4/25
 * Description: 流程实例查询
 */
package com.syc.activiti.chapter9;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;

/**
 * @className: ProcessInstanceQuery
 * @description: 流程实例查询
 * @date: 2020/4/25
 * @author: cakin
 */
@Slf4j
public class ProcessInstanceQuery {

    public static void main( String[] args ) {
        // 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 得到流程存储服务实例
        RepositoryService repositoryService = engine.getRepositoryService();
        // 得到运行时服务组件
        RuntimeService runtimeService = engine.getRuntimeService();
        // 部署流程描述文件
        repositoryService.createDeployment()
                .addClasspathResource("bpmn/chapter9/ProcessInstanceQuery.bpmn")
                .deploy();
        ProcessInstance pi1 = runtimeService.startProcessInstanceByKey(
                "testProcess", "key1");
        ProcessInstance pi2 = runtimeService.startProcessInstanceByKey(
                "testProcess", "key2");
        ProcessInstance pi3 = runtimeService.startProcessInstanceByKey(
                "testProcess", "key3");
        // 将流程置为中断状态
        runtimeService.suspendProcessInstanceById(pi1.getId());
        /***********************************************************
         **********************************************************/
        // 查询流程实例
        List<ProcessInstance> pis = runtimeService.createProcessInstanceQuery().processDefinitionKey("testProcess").list();
        log.info("使用processDefinitionKey方法查询流程实例：" + pis.size());
        pis = runtimeService.createProcessInstanceQuery().active().list();
        log.info("使用active方法查询流程实例：" + pis.size());
        pis = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey("key2").list();
        log.info("使用processInstanceBusinessKey方法查询流程实例：" + pis.size());
        // 根据多个流程实例ID查询
        Set<String> ids = new HashSet<>();
        ids.add(pi1.getId());
        ids.add(pi2.getId());
        pis = runtimeService.createProcessInstanceQuery().processInstanceIds(ids).list();
        log.info("使用processInstanceIds方法查询流程实例：" + pis.size());
    }

}
